<?php
/**
 * Description of AdvertiserController
 *
 * @author Admin
 */
class Adv_AdvertiserController extends Zend_Controller_Action 
{
    public function init() 
    {
        $this->_helper->layout->setLayout('advertiser');        
    }   
    
    public function indexAction() {
        $idAdvertiser = Zend_Auth::getInstance()->getIdentity()->id;
        $campaigns = new Adv_Model_Campaigns();        
        $allCampaigns = $campaigns->getLastActiveCampaigns();                
        $countView = $countClick = $costs =0;
        foreach ($allCampaigns as $key => $campaign) {
            //получение кол-ва просмотров кампании
            $view = $campaigns->getCountCampaignView($campaign['id']);
            foreach ($view as $countv) {
                $countView = $countv['count_view'];
            }
            //получение кол-ва кликов компании
            $click = $campaigns->getCountCampaignClick($campaign['id']);
            foreach ($click as $countc) {
                $countClick = $countc['count_click'];
            }
            $allCampaigns[$key]['count_view'] = $countView;
            $allCampaigns[$key]['count_click'] = $countClick;
            //текущие затраты = стартовый бюджет - текущий бюджет
            $costs = $allCampaigns[$key]['start_budget'] - $allCampaigns[$key]['current_budget'];
            $allCampaigns[$key]['costs'] = $costs;            
            $countView = $countClick = $costs = null;
        }
        $this->view->allcampaigns = $allCampaigns; 
        
        $active = 1; $notConfirmed = 2; $blocked = 3;
        /**
         * Статистика кампаний
         */
        $countCampaigns = $campaigns->getCountAllAdverCampaigns($idAdvertiser);
        $this->view->countСampaigns = $countCampaigns;
        $countActiveCampaigns = $campaigns->getCountStatusAdverCampaigns($idAdvertiser, $active);
        $countNotConfirmedCampaigns = $campaigns->getCountStatusAdverCampaigns($idAdvertiser, $notConfirmed);
        $countBlockedCampaigns = $campaigns->getCountStatusAdverCampaigns($idAdvertiser, $blocked);
        $this->view->countActiveCampaigns = $countActiveCampaigns;
        $this->view->countNotConfirmedCampaigns = $countNotConfirmedCampaigns;
        $this->view->countBlockedCampaigns = $countBlockedCampaigns; 
        
        /**
         * Статистика рекламных мест
         */
        
    }
       
    public function keywordsAction() {
        
    }
    public function bidAction () {
        
    }
    /**
     * профиль рекламодателя 
     */
    public function profileAction() {
        $form = new Application_Form_Updateprofile();
        $this->view->form = $form;
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        
        if ($id > 0) {
            $profile = new Adv_Model_Advertiser();
            $form->populate($adv = $profile->showProfile($id));

            if ($this->getRequest()->isPost()) {
                $postdata = $this->getRequest()->getPost();
                if ($form->isValid($postdata)) {
                    $data = array(
                        'password' => md5($form->getValue('password')),
                        'email' => $form->getValue('email'),
                        'phone' => $form->getValue('phone'),
                        'address' => $form->getValue('address'),
                        'company_name' => $form->getValue('company_name'),
                        'website' => $form->getValue('website')
                    );
                  
                    $profile->updateProfile($data, $id);
                    $this->_helper->redirector('profile', 'advertiser');
                }
            }
        }
        
    }
}

?>
